#import httplib
from google.appengine.api import urlfetch
import re

def obtain_zones():
  result = urlfetch.fetch( 'http://www.timegenie.com/timezones.php' )
  if result.status_code <> 200:
    return None

  raw = result.content

  head = r'<table width="100%" cellspacing="0" cellpadding="0" class="times">'
  tail = r'</table>'

  start = raw.find( head )
  end = raw.find( tail, start )

  sub_raw = raw[ start : end ].replace( head, '' )\
                              .replace( '<tr>', '' )\
                              .replace( '</tr>', '' )\
                              .strip()

  re_extract = re.sub( '\s+', ' ', # replace spaces
    re.sub( '\r\n', '', # replace new line
      re.sub( '<td>.*</p></td>', '', # replace back to top
        re.sub( '<td><b>.*</b></td>', '', # replace table header
          re.sub( '<td.*">', '<td>', # replace <td ...>
            re.sub( '<a.*</a>', '', sub_raw ) # replace <a name>
          )
        )
      )
    )
  ).strip()

  splits = re_extract.split( '</td> <td>' )
  splits[0] = splits[0].replace( '<td>', '' )
  splits[len(splits) - 1] = splits[len(splits) - 1].replace( '</td>', '' )

  zones = []
  for i in range( 0, len(splits), 3 ):
    zones.append( ( splits[i], splits[i+1], splits[i+2] ) )

  return zones
# end of obtain_zones
